/*
 * Attempt to solve the Best Sums Problem.
 */
#include "stdafx.h"
using namespace std;

struct psum {
	int number;
	int position;
};

inline bool operator<(psum a, psum b)  {
	if(a.number < b.number)
		return true;
	else
		if(a.number > b.number) return false;
		else return a.position < b.position;
}
inline bool operator<(psum a, int b)  {
	return a.number < b;
}
inline bool operator<(int a, psum b)  {
	return a < b.number;
}

inline bool operator==(psum a, psum b)  {
	return a.number == b.number;
}

void best_sums_main()  {
	int n, min, p;
	cin >> n >> min >> p;

	// now input the numbers;
	vector<psum> numbers(n);
	for(int i = 0; i < n; i++)  {
		cin >> numbers[i].number;
		numbers[i].position = i;

		if(i >= 1)
			numbers[i].number += numbers[i-1].number;

		numbers[i].number %= p;
	}
	sort(numbers.begin(), numbers.end());

	int prev = -1;
	int mindiff = p;
	typedef vector<psum>::iterator it;

	for(it i = numbers.begin(); i != numbers.end(); i++)  {
		if(i->number != prev)  {
			int target = min + i->number;

			it pos = lower_bound<it>(i+1, numbers.end(), target);
			for(; pos != numbers.end() && pos->position < i->position; pos++);
			
			if(pos != numbers.end())  {
				int thisdiff = pos->number - i->number;
				if(thisdiff < mindiff) mindiff = thisdiff;
				if(mindiff == min) break;
			}

			prev = i->number;
		}
	}
	cout << mindiff;
}